﻿using System;
using System.Globalization;
using CkSoftware.GroupMe.Sdk.Core.Dao;

namespace CkSoftware.GroupMe.Sdk.Core.Exceptions
{
	/// <summary>
	/// Global exception for any GroupMe related error.
	/// </summary>
	public class GroupMeException : Exception
	{
		private const string ExceptionFormat = "Error occured on executing GroupMe Api Request. Error Code '{0}', Errors: {1}";
		private readonly MetaData _metaData;

		/// <summary>
		/// Instantiates a new <see cref="GroupMeException" /> with the given <see cref="MetaData" />.
		/// </summary>
		/// <param name="meta">The metadata used for this exception.</param>
		public GroupMeException(MetaData meta)
			: base(
				string.Format(CultureInfo.CurrentCulture,
					ExceptionFormat,
					meta.Code,
					meta.Errors != null
						? string.Join(", ", meta.Errors)
						: !string.IsNullOrEmpty(meta.SingleError) ? meta.SingleError : "unknown error"))
		{
			_metaData = meta;
		}

		/// <summary>
		/// The metadata supplied by the GroupMe API.
		/// </summary>
		public MetaData MetaData
		{
			get { return _metaData; }
		}
	}
}